﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace ControlDesktop
{
    //[ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        public static extern bool InvalidateRect(IntPtr hwnd, IntPtr lpRect, bool bErase);

        static readonly string filePath = Application.StartupPath + "\\IconConfig.ini";
        static string tempFilePath = "C:\\TempFilePath";
        static readonly string section = "Icon";
        static List<Icon> iconList;
        static DesktopIcon desktopIcon;
        static IniFile iniFile;
        static bool is32bit;
        static Point[] Points = new Point[22];

        private void Form1_Load(object sender, EventArgs e)
        {
            //判断是否是32位系统
            is32bit = IntPtr.Size == 4;
            desktopIcon = new DesktopIcon(is32bit);
            iconList = desktopIcon.GetDesktopIconList();
            //将图标信息保存到文件中
            iniFile = new IniFile(filePath);
            if (!File.Exists(filePath))
            {
                foreach (Icon icon in iconList)
                {
                    iniFile.WriteValue(section + icon.Index, "Name", icon.Name);
                    iniFile.WriteValue(section + icon.Index, "Point-X", icon.Point.X.ToString());
                    iniFile.WriteValue(section + icon.Index, "Point-Y", icon.Point.Y.ToString());
                    iniFile.WriteValue(section + icon.Index, "Index", icon.Index.ToString());
                    iniFile.WriteValue(section + icon.Index, "hWnd", icon.hWnd.ToString());
                    iniFile.WriteValue(section + icon.Index, "Size", icon.Size.ToString());
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            RestoreDesktopIcon();
            Application.ExitThread();
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //设置爱心的坐标
            SetLovePoint();
            //新建临时文件的路径
            if (!Directory.Exists(tempFilePath))
            {
                Directory.CreateDirectory(tempFilePath);
            }
            int count = iconList.Count;
            //将多余的桌面图标移除
            for (int i = 0; i < count - 22; i++)
            {
                FileInfo fileInfo;
                string filePath = desktopIcon.GetDesktopPath() + "\\" + iconList[i].Name;
                if (File.Exists(filePath))
                {
                    fileInfo = new FileInfo(filePath);
                    fileInfo.MoveTo(tempFilePath + "\\" + iconList[i].Name);
                    continue;
                }
                if (File.Exists(filePath + ".lnk"))
                {
                    fileInfo = new FileInfo(filePath + ".lnk");
                    fileInfo.MoveTo(tempFilePath + "\\" + iconList[i].Name + ".lnk");
                    continue;
                }
                if (Directory.Exists(filePath + "\\" + iconList[i].Name))
                {
                    DirectoryInfo info = new DirectoryInfo(filePath + "\\" + iconList[i].Name);
                    info.MoveTo(tempFilePath + "\\" + iconList[i].Name);
                    continue;
                }
            }
            for (int i = 0; i < 22; i++)
            {
                desktopIcon.SetDesktopIconPoint(iconList[count - 22 + i].Name, new Point() { X = Points[i].X, Y = Points[i].Y });
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(tempFilePath);
            foreach (FileSystemInfo fi in dir.GetFileSystemInfos())
            {
                if (fi.Attributes == FileAttributes.Directory)
                {
                    Directory.Move(fi.FullName, desktopIcon.GetDesktopPath() + "\\" + fi.Name);
                }
                else
                {
                    FileInfo fileInfo = new FileInfo(fi.FullName);
                    fileInfo.MoveTo(desktopIcon.GetDesktopPath() + "\\" + fileInfo.Name);
                }
            }
            InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
        }

        //还原桌面图标
        private void RestoreDesktopIcon()
        {
            foreach (Icon icon in iconList)
            {
                //Name
                string name = iniFile.Readvalue(section + icon.Index, "Name");
                //Point-X
                string strPointX = iniFile.Readvalue(section + icon.Index, "Point-X");
                int pointx;
                int.TryParse(strPointX, out pointx);
                //Point-Y
                string strPointY = iniFile.Readvalue(section + icon.Index, "Point-Y");
                int pointy;
                int.TryParse(strPointY, out pointy);
                //修改桌面图标的位置
                desktopIcon.SetDesktopIconPoint(name, new Point() { X = pointx, Y = pointy });
            }
        }

        //设置爱心的坐标
        private void SetLovePoint()
        {
            Points[0] = new Point(319, 114);
            Points[1] = new Point(988, 83);
            Points[2] = new Point(973, 270);
            Points[3] = new Point(510, 508);
            Points[4] = new Point(731, 555);
            Points[5] = new Point(861, 428);
            Points[6] = new Point(929, 37);
            Points[7] = new Point(819, 511);
            Points[8] = new Point(321, 201);
            Points[9] = new Point(1006, 193);
            Points[10] = new Point(463, 77);
            Points[11] = new Point(552, 107);
            Points[12] = new Point(657, 595);
            Points[13] = new Point(380, 55);
            Points[14] = new Point(585, 552);
            Points[15] = new Point(359, 280);
            Points[16] = new Point(931, 354);
            Points[17] = new Point(401, 367);
            Points[18] = new Point(468, 446);
            Points[19] = new Point(645, 132);
            Points[20] = new Point(843, 52);
            Points[21] = new Point(751, 90);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            RestoreDesktopIcon();
        }
    }
}
